package com.gp.learn.ssm.base.system.service.impl;

import com.gp.learn.ssm.base.core.mybaits.base.service.impl.BaseServiceImpl;
import com.gp.learn.ssm.base.core.mybaits.customwrapper.CriteriaQuery;
import com.gp.learn.ssm.base.core.security.model.UserAccount;
import com.gp.learn.ssm.base.core.security.model.UserInfo;
import com.gp.learn.ssm.base.system.entity.BaseAccount;
import com.gp.learn.ssm.base.system.mapper.BaseAccountMapper;
import com.gp.learn.ssm.base.system.service.BaseAccountService;
import com.gp.learn.ssm.base.system.service.BaseUserService;
import com.gp.learn.ssm.common.utils.FlymeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统用户-登录账号 服务实现类
 *
 * @author gp
 * @since 2020-07-27
 */
@Service
public class BaseAccountServiceImpl extends BaseServiceImpl<BaseAccountMapper, BaseAccount> implements BaseAccountService {

    @Autowired
    private BaseUserService baseUserService;


    @Override
    public UserAccount login(String accountName) {
        if (FlymeUtils.isEmpty(accountName)) {
            return null;
        }
        BaseAccount baseAccount = findByAccountName(accountName);
        UserAccount userAccount = new UserAccount();
        BeanUtils.copyProperties(baseAccount, userAccount);
        if (FlymeUtils.isNotEmpty(baseAccount)) {
            Long userId = baseAccount.getUserId();
            UserInfo userProfile = baseUserService.getUserWithAuthoritiesById(userId);
            userAccount.setUserProfile(userProfile);
        }
        return userAccount;
    }

    @Override
    public BaseAccount findByAccountName(String accountName) {
        CriteriaQuery cq = new CriteriaQuery(BaseAccount.class);
        cq.select("*");
        cq.eq("account", accountName);
        return getOne(cq);
    }
}
